import { navyMenu } from '@/utils/navyMenu';
import { capitalizeFirstLetter } from '@/utils/methods';
import { flowableFormChildrenMap } from './flowableForm';
import type { RouteRecordRaw } from 'vue-router';

const modules = import.meta.glob('../views/**/*.vue');

const extraRoutes: RouteRecordRaw[] = [];

const flowableFormChildren: RouteRecordRaw[] = [];

extraRoutes.push({
  path: 'home',
  name: 'Home',
  meta: { title: '网站首页' },
  component: () => import('@/views/home/index.vue')
});

//加载全部的flowableForm的路由
flowableFormChildrenMap.forEach((title, key) => {
  flowableFormChildren.push({
    path: key,
    name: 'flowableForm' + capitalizeFirstLetter(key),
    meta: { title: title, header: '新建' + title },
    component: () => import(`../views/flowable/form/${key}/index.vue`)
  });

  flowableFormChildren.push({
    path: key + '/:processInstanceId',
    name: 'flowableForm' + capitalizeFirstLetter(key) + 'Instance',
    meta: { title, header: '查看' + title },
    component: () => import(`../views/flowable/form/${key}/Instance.vue`),
    props: true
  });
});

extraRoutes.push({
  path: 'flowable/form',
  name: 'FlowableForm',
  meta: { title: '流程表单' },
  children: flowableFormChildren
});

navyMenu.forEach(subMenu => {
  const subMenuRoute: RouteRecordRaw = {
    path: subMenu.path,
    name: subMenu.name,
    meta: { title: subMenu.title },
    children: [],
    redirect: ''
  };
  let index = 0;
  subMenu.children?.forEach(menu => {
    const itemRoute: RouteRecordRaw = {
      path: menu.path,
      name: menu.name,
      meta: { title: menu.title },
      component: modules[`../views${menu.fullPath}/index.vue`]
    };
    subMenuRoute.children?.push(itemRoute);
    ++index === 1 && (subMenuRoute.redirect = menu.fullPath as string);
  });
  subMenuRoute.children && subMenuRoute.children.length > 0 && extraRoutes.push(subMenuRoute);
});

extraRoutes.push({
  path: ':pathMatch(.*)*',
  name: 'NotFound',
  meta: { title: '未知网页' },
  redirect: '/404'
});

extraRoutes.push({
  path: '404',
  name: 'NotFoundRe',
  meta: { title: '未知网页' },
  component: () => import('@/views/common/NotFound.vue')
});

const routes: Readonly<RouteRecordRaw[]> = [
  {
    path: '/login',
    name: 'Login',
    meta: { title: '登录页面' },
    component: () => import('@/views/common/Login.vue')
  },
  {
    path: '/',
    name: 'Layout',
    meta: { title: '网站主页' },
    component: () => import('@/layout/index.vue'),
    redirect: '/home',
    children: extraRoutes
  }
];

export default routes;
